package com.telecomyt.jwportal.utils;

import android.os.Handler;
import android.os.HandlerThread;

/**
 * @author created by chengDa
 * @time 2020/9/15  10:01
 * @description 该类为连续点击两次退出键退出的工具类
 */
public class TwoClickBackExit {
    /**
     * 退出设置单列模式
     */
    private static TwoClickBackExit instance;

    private TwoClickBackExit() {
    }

    public static TwoClickBackExit getInstance() {
        if (instance == null) {
            instance = new TwoClickBackExit();
        }
        return instance;
    }

    /**
     * 点击时是否退出
     */
    private boolean isExit = false;

    /**
     * 更改标记为不退出
     */
    private Runnable task = new Runnable() {
        @Override
        public void run() {
            isExit = false;
        }
    };

    /**
     * 更改isExit为true 并且开启计时，两秒后更改isExit为false
     */
    public void doExitInOneSecond() {
        isExit = true;
        HandlerThread thread = new HandlerThread("doTask");
        thread.start();
        new Handler(thread.getLooper()).postDelayed(task, 1500);
    }

    /**
     * 调用此方法来判断是否退出 如果不退出(isExit为false)则调用doExitInOneSecond()
     *
     * @return
     */
    public boolean isExit() {
        return isExit;
    }

    public void setExit(boolean isExit) {
        this.isExit = isExit;
    }
}